package com.kxf.pushkeepalive.service;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.kxf.pushkeepalive.MainActivity;
import com.kxf.pushkeepalive.R;

import static com.huawei.hms.push.constant.RemoteMessageConst.Notification.CHANNEL_ID;

public class MyForeService extends Service {
    public MyForeService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyForeService", "onCreate");
        setForegroundService();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Log.d("MyForeService", "running=====");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    /**
     * 通过通知启动服务
     */
    @TargetApi(Build.VERSION_CODES.O)
    public void setForegroundService() {
        //设定的通知渠道名称
        String channelName = "语音播报";
        //设置通知的重要程度
        int importance = NotificationManager.IMPORTANCE_LOW;
        //构建通知渠道
        NotificationChannel channel = new NotificationChannel("sound prompt", channelName, importance);
        channel.setDescription("描述");
        //在创建的通知渠道上发送通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "sound prompt");
        builder.setSmallIcon(R.mipmap.mylogo) //设置通知图标
                .setContentTitle("设置通知标题")//设置通知标题
                .setContentText("设置通知内容")//设置通知内容
                .setAutoCancel(false) //用户触摸时，自动关闭
//                .setOngoing(true)//设置处于运行状态
                .setGroupSummary(false)
                .setGroup("sound");
        //向系统注册通知渠道，注册后不能改变重要性以及其他通知行为
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
        //将服务置于启动状态 NOTIFICATION_ID指的是创建的通知的ID
        startForeground(82, builder.build());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.d("MyForeService", "onDestroy====");
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.d("MyForeService", "onTaskRemoved===");
        super.onTaskRemoved(rootIntent);
    }
}
